import datetime
import json
import time

from dateutil.tz import tzutc
from kubernetes import client, watch, config
from kubernetes.client.rest import ApiException

from airflow import AirflowException



# kubeconfig_path = "/Users/lianbin/.kube/config"
kubeconfig_path = "/Users/lianbin/WorkFile/dev/config"
config.load_kube_config(config_file=kubeconfig_path)
# 创建 Kubernetes API 客户端
custom_api = client.CustomObjectsApi()

w = watch.Watch()
hostname = "my-first-application-cluster"
rv = '0'
stop = False
api_version = "flink.apache.org/v1beta1"
kind = "FlinkDeployment"

while(True):
    try:
        rv = '0'
        retry_timestamp = int(time.time())
        j_retry = 0
        # c = v1.list_namespaced_custom_object( namespace="default",
        #                                  group="flink.apache.org",
        #                                  version="v1beta1",
        #                                  plural="flinkdeployments")
        watch_event = w.stream(custom_api.list_namespaced_custom_object,
                               namespace="default",
                                         group="flink.apache.org",
                                         version="v1beta1",
                                         plural="flinkdeployments",
                                         resource_version=rv
                               )
        for event in watch_event:
            print(event)
        if stop:
            break
    except ApiException as e:
        print(e)



w.stop()
print("end")

